<template>
  <AdminLayout>
    <el-form
      :model="ArticleForm"
      :rules="rules"
      ref="ruleForm"
      label-width="100px"
      class="demo-ruleForm"
    >
      <el-form-item label="修改标题" prop="title">
        <el-input v-model="ArticleForm.title"></el-input>
      </el-form-item>

      <el-form-item label="修改描述" prop="desc">
        <el-input type="textarea" v-model="ArticleForm.desc"></el-input>
      </el-form-item>

      <div>
        <h3>修改详情</h3>
        <QuillEditor
          theme="snow"
          contentType="html"
          v-model:content="ArticleForm.details"
        ></QuillEditor>
      </div>

      <el-form-item label="修改发布人" prop="author">
        <el-select v-model="ArticleForm.author" placeholder="请选择选择发布人">
          <el-option label="陕西日报" value="shanghai"></el-option>
          <el-option label="北京日报" value="beijing"></el-option>
        </el-select>
      </el-form-item>

      <el-form-item label="修改时间" required>
        <el-col>
          <el-form-item prop="created_at">
            <el-date-picker
              type="date"
              placeholder="选择日期"
              v-model="ArticleForm.created_at"
            ></el-date-picker>
          </el-form-item>
        </el-col>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" @click="submitForm('ruleForm')"
          >立即修改</el-button
        >
        <el-button @click="resetForm('ruleForm')">重置</el-button>
      </el-form-item>
    </el-form>
  </AdminLayout>
</template>

<script>
import { QuillEditor } from "@vueup/vue-quill";
import "@vueup/vue-quill/dist/vue-quill.snow.css";

import { mapActions, mapState } from "vuex";

export default {
  components: {
    QuillEditor,
  },
  computed: {
    ...mapState("Item", ["Item"]),
  },
  name: "ArticleCreation",
  data() {
    return {
      ArticleForm: {
        title: "",
        desc: "",
        details: "",
        author: "",
        created_at: "",
      },
      rules: {
        title: [
          { required: true, message: "请输入文章标题", trigger: "blur" },
          {
            min: 5,
            max: 15,
            message: "长度在 5 到 15 个字符",
            trigger: "blur",
          },
        ],
        desc: [
          { required: true, message: "请填写文章描述", trigger: "change" },
        ],
        author: [
          { required: true, message: "请选择发布人", trigger: "change" },
        ],
        created_at: [
          {
            type: "date",
            required: true,
            message: "请选择日期",
            trigger: "change",
          },
        ],
        details: [
          { required: true, message: "请填写文章详情", trigger: "change" },
        ],
      },
    };
  },
  async created() {
    // articleForm
    // 数据回填
    const { id } = this.$route.params;
    await this.GetItem(id);
    this.ArticleForm = this.Item;
    console.log(id);
  },

  methods: {
    ...mapActions("Item", ["GetItem"]),

    submitForm(formName) {
      this.$refs[formName].validate(async (valid) => {
        if (valid) {
          try {
            await this.$store.dispatch("Item/updateArticle", {
              id: this.$route.params.id,
              data: this.ArticleForm,
            });
            this.$message.success("修改成功");

            this.$router.push({
              name: "Article",
            });
          } catch (e) {
            this.$message.success("网络错误");
          }
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    },
  },
};
</script>

<style lang="stylus">

.ql-container
  height: 300px;
</style>
